
<div class="templatemo-content-wrapper">
	<div class="templatemo-content">
		<ol class="breadcrumb">
			<li><a href="<?=site_url()?>/cost/index">成本分析</a></li>
			<li class="active">添加库存损耗</li>
		</ol>
		<div class="row margin-bottom-30">
			<div class="col-md-12">
				<ul class="nav nav-pills">
					<li class="active"><a href="<?=site_url()?>/cost/lostindex">返回库存损耗列表</a></li>
				</ul>
			</div>
		</div>
		<div class="row">
			<div class="col-md-12">
				<div class="table-responsive">
					<h4 class="margin-bottom-15">添加库存损耗</h4>
					<form role="form" id="templatemo-preferences-form"
						onsubmit="return false;">
						<div class="row">
							<div class="col-md-6 margin-bottom-15">
								<label for="firstName" class="control-label">商品名称</label> <input
									type="text" class="form-control" id="name" value="">
							</div>
							<div class="col-md-6 margin-bottom-15">
								<label for="lastName" class="control-label">数量</label> <input
									type="text" class="form-control" id="amount" value="">
							</div>
							<div class="col-md-6 margin-bottom-15">
								<label for="firstName" class="control-label">收入(元)</label> <input
									type="text" class="form-control" id="total" value="">
							</div>
							<div class="col-md-6 margin-bottom-15">
								<label for="lastName" class="control-label">类型</label> 
								<select class="form-control margin-bottom-15" id="type">
                					<option value="1">商家线下交易</option>
                					<option value="2">库存损耗</option>
                					<option value="3">其它</option>
                  				</select>
							</div>
							<div class="col-md-6 margin-bottom-15">
								<label for="firstName" class="control-label">备注</label> <input
									type="text" class="form-control" id="note" value="">
							</div>
							<div class="col-md-6 margin-bottom-15">
								<label for="lastName" class="control-label">时间</label> <input
									type="text" class="form-control" id="date" value="<?php echo date('Y-m-d',time()); ?>">
							</div>
						</div>
						<div class="row templatemo-form-buttons">
							<div class="col-md-12">
								<button type="submit" class="btn btn-primary" id="submit_btn">添加</button>
							</div>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
	$(document).ready(function() {
		//var cache = {};
		$("#name").autocomplete({
			minLength:1,
			source:function(request,response){
				/*var term = request.term;
				if ( term in cache ) {
         			 response( cache[ term ] );
         			 return;
       			 }*/
       			 $.ajax({
                    url:'<?php echo site_url('product/getShowProduct')?>',
                    type : "post",
                    dataType: "json",
                    data:{
                        name: request.term
                    },
                    success: function( data ) {
                        //cache[ term ] = data;
                        response($.map(data, function(item) {
    		            return {
    		                 label: item.name+'-'+item.description,
    		                 value: item.name+ '-' + item.id,
    		                 price: item.price,
    		             }
    		         }));
                    }
                });
			},
			select: function( event, ui ) {
        		$( "#total" ).val( ui.item.price );
      		}
		});
		$("#submit_btn").click(function() {
			var name = $("#name").val();
			var amount = $("#amount").val();
			var total = $("#total").val();
			var type = $("#type").val();
			var note = $("#note").val();
			var date = $("#date").val();
			$.ajax({
				url : '<?php echo site_url('cost/savelost')?>',
				type : "post",
				async : true,
				data : {
					'name' : name,
					'amount' : amount,
					'total' : total,
					'type' : type,
					'note' : note,
					'date' : date
				},
				dataType : "json",
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
						window.location.reload();
					}else{
						alert(data.msg);
					}
				}
			});
		});
		$('#date').datepicker({
			dateFormat: 'yy-mm-dd',
			monthNames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
			dayNamesMin: ['日','一','二','三','四','五','六']
		});
	}); 
</script>